package com.neuronbit.businessflow.intellij.lang;

import com.intellij.lang.ASTNode;
import com.intellij.psi.PsiElement;
import org.antlr.intellij.adaptor.psi.ANTLRPsiNode;
import org.jetbrains.annotations.Nullable;

import java.util.List;

import static com.neuronbit.businessflow.intellij.lang.BpmParserDefinition.PARAM;

/**
 * The type Psi param.
 */
public class PsiParam extends ANTLRPsiNode {
    /**
     * Instantiates a new Psi param.
     *
     * @param node the node
     */
    public PsiParam(ASTNode node) {
        super(node);
    }

    /**
     * Gets as.
     *
     * @return the as
     */
    public PsiAttribute getAs() {
        return getAttribute("as");
    }

    @Nullable
    private PsiAttribute getAttribute(String name) {
        List<PsiElement> childrenByType = findChildrenByType(PARAM);
        for (PsiElement attr : childrenByType) {
            if (((PsiAttribute) attr).getAttrName().equalsIgnoreCase(name)) {
                return ((PsiAttribute) attr);
            }
        }

        return null;
    }

    /**
     * Gets value.
     *
     * @return the value
     */
    public PsiAttribute getValue() {
        return getAttribute("value");
    }

    /**
     * Gets type.
     *
     * @return the type
     */
    public PsiAttribute getType() {
        return getAttribute("type");
    }
}
